﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DatabaseComponent;

public partial class examControlAR : System.Web.UI.UserControl
{

    public bool showButton
    {
        get
        {
            return btnNext.Visible;
        }
        set
        {
            btnNext.Visible = value;
        }
    }

    public Questions objQuestions
    {
        get
        {
            if (ViewState["objQuestions"] == null)
            {
                ViewState["objQuestions"] = System.Guid.NewGuid().ToString();
            }

            if (Session[ViewState["objQuestions"].ToString()] == null)
            {
                Session[ViewState["objQuestions"].ToString()] = new Questions();
            }

            return (Questions)Session[ViewState["objQuestions"].ToString()];

        }
        set
        {
            if (ViewState["objQuestions"] == null)
            {
                ViewState["objQuestions"] = System.Guid.NewGuid().ToString();
            }

            Session[ViewState["objQuestions"].ToString()] = value;

        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {

    }


    /// <summary>
    /// this function will set the control to a specific question
    /// </summary>
    /// <param name="QuestionsID">the id of the question to set this control to</param>
    public void set(long QuestionsID)
    {
        QuestionsDB objQuestionsDB = new QuestionsDB(ConnectionStrings.LMS);
        List<Questions> lstQuestions = objQuestionsDB.getQuestionsByID(QuestionsID);
        if (lstQuestions.Count == 0)
            objQuestions = new Questions();
        else
        {
            objQuestions = lstQuestions[0];
        }


        bindControls();


    }


    /// <summary>
    /// this function will bind the controls with values
    /// </summary>
    private void bindControls()
    {
        if (objQuestions.ID == -1)
        {
            cblAnswers.Visible = false;
            rblAnswers.Visible = false;

            lblQuestionText.Text = "خطأ , لا يوجد سؤال !";

        }
        else
        {

            List<QuestionsAnswers> lstQuestionsAnswers = objQuestions.lstQuestionsAnswers;

            lblQuestionText.Text = "";

            cblAnswers.Items.Clear();
            rblAnswers.Items.Clear();



            lblQuestionText.Text = objQuestions.QuestionAR;
            

            long lAnswerCount = 0;
            foreach (QuestionsAnswers obj in lstQuestionsAnswers)
            {
                if (obj.IsAnswer)
                    lAnswerCount++;

            }


            if (lAnswerCount == 1)
            {
                rblAnswers.Visible = true;
                cblAnswers.Visible = false;
                
                


                rblAnswers.DataSource = lstQuestionsAnswers;

                rblAnswers.DataTextField = "AnswerAR";
                rblAnswers.DataValueField = "ID";

                rblAnswers.DataBind();


            }
            else
            {
                rblAnswers.Visible = false;
                cblAnswers.Visible = true ;




                cblAnswers.DataSource = lstQuestionsAnswers;

                cblAnswers.DataTextField = "AnswerAR";
                cblAnswers.DataValueField = "ID";

                cblAnswers.DataBind();
            }



        }
    }

    /// <summary>
    /// this function will return the current selected answer
    /// </summary>
    /// <returns>a string containing answers seperated by ;</returns>
    public string getAnswer()
    {

        string strRet = "";

        if (rblAnswers.Items.Count > 0)
        {
            foreach (ListItem li in rblAnswers.Items)
            {
                if (li.Selected)
                {
                    strRet = strRet + li.Value + ";";
                }



            }

            if (strRet != "")
                strRet = strRet.Substring(0, strRet.LastIndexOf(";"));

        }
        else
        {

            foreach (ListItem li in cblAnswers.Items)
            {
                if (li.Selected)
                {
                    strRet = strRet + li.Value + ";";
                }



            }

            if (strRet != "")
                strRet = strRet.Substring(0, strRet.LastIndexOf(";"));

        }





        return strRet;

    }


    /// <summary>
    /// this function will set the answers to question in control
    /// </summary>
    /// <param name="strAnswers">the answer to set</param>
    public void setAnswer(string strAnswers)
    {
        if (strAnswers == "")
            return;

        List<long> lstSelectedAnsweres = new List<long>();

        string[] strSplitted = strAnswers.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

        lstSelectedAnsweres = new List<long>(from obj in strSplitted select long.Parse (obj));


        if (lstSelectedAnsweres.Count > 1)
        {
            foreach (ListItem li in cblAnswers.Items)
            {
                if (lstSelectedAnsweres.Contains(long.Parse(li.Value)))
                    li.Selected = true;
            }
        }
        else
        {
            foreach (ListItem li in rblAnswers.Items)
            {
                if (lstSelectedAnsweres.Contains(long.Parse(li.Value)))
                    li.Selected = true;
            }
        }

    }




    /// <summary>
    /// this function will return a value indicating that the user choosed the correct answer or not
    /// </summary>
    /// <returns>a true for correct answer</returns>
    public bool get()
    {
       


        if (objQuestions.ID == -1)
        {
            return false;
        }



        List<QuestionsAnswers> lstUserAnswers = new List<QuestionsAnswers>();


        List<QuestionsAnswers> lstRealAnswers = new List<QuestionsAnswers> ( );


        foreach (QuestionsAnswers obj in objQuestions.lstQuestionsAnswers)
        {
            if (obj.IsAnswer)
            {
                lstRealAnswers.Add(obj);
            }
        }

        if (cblAnswers.Visible)
        {
            foreach (ListItem li in cblAnswers.Items)
            {
                if (li.Selected)
                {
                    QuestionsAnswers qa = new QuestionsAnswers();
                    qa.ID = long.Parse (li.Value);

                    lstUserAnswers.Add(qa);
                }
            }
        }
        else
        {
            foreach (ListItem li in rblAnswers.Items)
            {
                if (li.Selected)
                {
                    QuestionsAnswers qa = new QuestionsAnswers();
                    qa.ID = long.Parse(li.Value);

                    lstUserAnswers.Add(qa);
                }
            }
        }

        foreach (QuestionsAnswers obj in lstUserAnswers)
        {
            if (!lstRealAnswers.Contains(obj))
            {
                return false;
            }

            lstRealAnswers.Remove(obj);


        }


        if (lstRealAnswers.Count > 0)
        {
            return false;
        }






        return true;
    }




    public delegate void CommandDel(object sender,CustomEventArgs e);

    public event CommandDel Command = null;


    private void raiseCommand(CustomEventArgs e)
    {
        if (Command != null)
        {
            Command(this, e);
        }
    }



    protected void btnNext_Click(object sender, EventArgs e)
    {
        CustomEventArgs ea = new CustomEventArgs();
        ea.objArgs = get();

        raiseCommand(ea);

    }
}